<?php

namespace App\Http\Controllers\Admin;

use App\Models\Banner;
use App\Models\BClass;
use App\Models\Config;
use App\Models\Version;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ConfigController extends Controller
{
    public function aboutus(Request $request)
    {
        if($request->isMethod('post')){
            Config::where('name','ABOUTUS')->update(['value'=>$request->get('aboutus')]);
            return back()->with(['message'=>"修改成功"]);
        }

        $about = Config::where('name','ABOUTUS')->first();
        return view('admin.aboutus',['about'=>$about]);
    }

    public function integral(Request $request)
    {
        if($request->isMethod('post')){
            Config::where('name','INTERULE')->update(['value'=>$request->get('interule')]);
            return back()->with(['message'=>"修改成功"]);
        }

        $interule = Config::where('name','INTERULE')->first();
        return view('admin.interule',compact('interule'));
    }

    //运费价格


    public function interule_rule(Request $request)
    {
        $jsdh = Config::where('name','JFDH')->first();
        $jfsc = Config::where('name','JFSC')->first();
        $freght = Config::where('name','YF')->first();
        $share = Config::where('name','SHARE')->first();
        $class = BClass::all();
        if($request->has('jfdh')){
            $jsdh->value = $request->get('jfdh');
            $jsdh->save();
            return back()->with(['message'=>"修改成功"]);
        }
        if($request->has('jfsc')){
            $jfsc->value = $request->get('jfsc');
            $jfsc->save();
            return back()->with(['message'=>"修改成功"]);
        }
        if($request->has('freght')){
            $freght->value = $request->get('freght');
            $freght->save();
            return back()->with(['message'=>"修改成功"]);
        }
        if($request->has('share')){
            $share->value = $request->get('share');
            $share->save();
            return back()->with(['message'=>"修改成功"]);
        }
        if($request->has('class')){

        }

        return view('admin.interule_rule',['jfdh'=>$jsdh,'jfsc'=>$jfsc,'freght'=>$freght,'share'=>$share,'class'=>$class]);
    }
    ///banner
    public function banner_list(Request $request)
    {
        if($request->isMethod('post')){
            if($request->type=='del'){
                Banner::destroy($request->get('banner_id'));
                return redirect()->back()->with(['message'=>"删除成功"]);
            }
            if($request->type=='ins'){
                $banner = new Banner();
                $img = $this->upload_img($request->banner,'upload/banner',5);
                $banner->img = $img['img']['pic_id'];
                $banner->type = 1;
                $banner->detail = $request->get('detail');
                $banner->sort = $request->get('sort',0);
                $banner->save();
                return redirect("config/banner")->with(['message'=>"添加成功"]);
            }
        }

        $banner = Banner::orderBy('sort')->get();
        return view('admin.banner_list',['banner'=>$banner]);
    }

    public function banner_create(Request $request)
    {
        return view('admin.banner_create');
    }

    public function b_banner(Request $request)
    {
        if($request->isMethod('post')){
            if($request->hasFile('banner')){
                $banner = $request->banner->store('upload/b_banner');
                DB::table('b_bander')->update([
                   'image'=>env('APP_URL')."/".$banner,
                ]);
                return redirect()->back()->with(['message'=>"修改成功"]);
            }
            if($request->has('url')){
                DB::table('b_bander')->update([
                   'url'=>$request->get('url'),
                ]);
                return redirect()->back()->with(['message'=>"修改成功"]);
            }
        }

        $banner = DB::table('b_bander')
            ->first();
        return view('admin.b_banner',['banner'=>$banner]);
    }

    //APP版本控制
    public function version_list(Request $request)
    {
//添加
        if($request->isMethod('post')){
            $input=$request->input();
            $name=$_FILES['app_file']['name'];
            $extension=strrchr($name,'.');
            $path = $request->app_file->storeAs('upload/app_installer', 'ezs_'.$input['version'].$extension);
            $app_config= new  Version();
            $app_config->name = $input['name'];
            $app_config->version = $input['version'];
            $app_config->app_path = $path;
            $app_config->app_ios_path = $input['app_ios_path'];
            $app_config->detail = $input['version_detail'];
            $app_config->type = $input['type'];
            if($app_config->save()){
                return back()->with('message',"添加成功");
            }
        }else{
            //列表
            $app = Version::orderBy('created_at','desc')->get();
            return view('admin.version_list',[
                'app'=>$app,
            ]);
        }
    }
    //删除版本
    public function version_del(Request $request)
    {
        if(Version::destroy($request->input('version_id'))){
            return ['status'=>1,'message'=>'删除成功'];
        }else{
            return ['status'=>0,'message'=>"删除失败"];
        }
    }
}
